package com.ruoyi.wx.controller;

import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.RedisUtils;
import com.ruoyi.common.utils.ScoreUtile;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.framework.web.service.PermissionService;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.wx.entity.PtExpClass;
import com.ruoyi.wx.entity.PtTeacherClass;
import com.ruoyi.wx.entity.dto.MyClassDTO;
import com.ruoyi.wx.entity.dto.OpenAndNoExpDTO;
import com.ruoyi.wx.entity.vo.PtReportVO;
import com.ruoyi.wx.entity.vo.PtScoreVO;
import com.ruoyi.wx.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.*;


/**
 * @author 莫修
 * @date 2022/3/3
 */

@RestController
@Api(value = "教师接口测试", tags = {"教师接口测试"})
@RequestMapping("/wx")
public class PtTeacherController extends BaseController {

    @Autowired
    private ISysUserService userService;

    @Autowired
    private PermissionService permissionService;

    @Autowired
    private IPtTeacherClassService ptTeacherClassService;

    @Autowired
    private IPtExpClassService ptExpClassService;

    @Autowired
    private IPtClassExpAllService ptClassExpAllService;

    @Autowired
    private IPtScoreService ptScoreService;

    @Autowired
    private IPtReportService ptReportService;


    @ApiOperation("教师验证码验证")
    @GetMapping("/verifyTeacherCode")
    public AjaxResult<Void> verifyTeacherCode(String teacherCode) {
        String cacheSet = RedisUtils.getCacheObject(Constants.TEACHER_CODE);
        if (!teacherCode.equals(cacheSet)) {
            return AjaxResult.error("验证码错误");
        }
        Long userId = SecurityUtils.getUserId();
        SysUser sysUser = userService.selectUserById(userId);
        sysUser.setRoleIds(new Long[]{Constants.SYS_TEACHER_ROLE});
        return toAjax(userService.updateUser(sysUser));
    }

    @ApiOperation("教师设置后台用户名密码")
    @GetMapping("/updateUsernameAndPassWord")
    public AjaxResult<Void> updateUsernameAndPassWord(String username, String password) {
        if (!permissionService.hasTeacherOrAdmin()) {
            return AjaxResult.error("没有权限");
        }
        Long userId = SecurityUtils.getUserId();
        SysUser sysUser = new SysUser();
        sysUser.setUserId(userId);
        userService.checkUserAllowed(sysUser);
        sysUser.setUserName(username);
        sysUser.setPassword(SecurityUtils.encryptPassword(password));
        return toAjax(userService.updateById(sysUser));
    }

    @ApiOperation("教师选择班级")
    @GetMapping("/chooseClass")
    public AjaxResult<Void> chooseClass(Long[] classId) {
        if (!permissionService.hasTeacherOrAdmin()) {
            return AjaxResult.error("没有权限");
        }
        if (classId == null || classId.length == 0) {
            return AjaxResult.error("所选班级不能为空");
        }
        Long teacherId = SecurityUtils.getUserId();
        List<PtTeacherClass> list = new ArrayList<>();
        for (Long aLong : classId) {
            list.add(PtTeacherClass.builder().build().setDeptId(aLong).setTeacherId(teacherId));
        }
        boolean b = ptTeacherClassService.chooseClass(teacherId, list);
        return b ? AjaxResult.success() : AjaxResult.error();
    }

    @ApiOperation("查看本班级的实验开启情况 分开启和未开启")
    @GetMapping("/getExpIsOpenByDeptId")
    public AjaxResult getExpIsOpenByDeptId(@ApiParam("班级id") Long classId) {
        if (!permissionService.hasTeacherOrAdmin()) {
            return AjaxResult.error("没有权限");
        }
        if (classId == null) {
            return AjaxResult.error("班级id不能为空");
        }
        long[] allClassId = ptClassExpAllService.getOneByClassId(classId);

        if (allClassId == null || allClassId.length == 0) {
            return AjaxResult.error("老师选择实验");
        }
        OpenAndNoExpDTO openAndNoExpDTO = ptExpClassService.getExpIsOpenByDeptId(classId, allClassId);

        return AjaxResult.success(openAndNoExpDTO);
    }

    @ApiOperation("老师开启实验")
    @GetMapping("/openExp")
    public AjaxResult openExp(@ApiParam("班级id") Long classId, @ApiParam("实验id") Long expId, @ApiParam("结束时间毫秒值") Long date) {
        if (!permissionService.hasTeacherOrAdmin()) {
            return AjaxResult.error("没有权限");
        }
        if (classId == null || expId == null) {
            return AjaxResult.error("班级或者实验不能为空");
        }
        if (date < System.currentTimeMillis()) {
            return AjaxResult.error("不能设置过期时间");
        }

        return toAjax(ptExpClassService.openExp(classId, expId, date));
    }

    @ApiOperation("老师查看自己的班级")
    @GetMapping("/getMyClass")
    public AjaxResult getMyClass() {
        if (!permissionService.hasTeacherOrAdmin()) {
            return AjaxResult.error("没有权限");
        }

        return AjaxResult.success(ptTeacherClassService.getMyClass());
    }

    @PostMapping("/scanCode")
    @ApiOperation("扫码打分")
    public AjaxResult scanCode(@Validated @RequestBody PtScoreVO ptScoreVO) {
        if (!permissionService.hasTeacherOrAdmin()) {
            return AjaxResult.error("没有权限");
        }
        if (!ScoreUtile.checkScore(ptScoreVO.getDiscipline()) || !ScoreUtile.checkScore(ptScoreVO.getOperate())) {
            AjaxResult.error("分数在0~100之间");
        }
        return toAjax(ptScoreService.scanCode(ptScoreVO));
    }

    @ApiOperation("是不是老师")
    @GetMapping("/isTeacher")
    public AjaxResult studentChoosesClass() {
        boolean res = false;
        if (permissionService.hasTeacherOrAdmin()) {
            res = true;
        }
        return AjaxResult.success(res);
    }

    @ApiOperation("实验报告给分")
    @PostMapping("/expReportGetScore")
    public AjaxResult expReportGetScore(@Validated @RequestBody PtReportVO ptReportVO) {
        if (!permissionService.hasTeacherOrAdmin()) {
            return AjaxResult.error("没有权限");
        }
        return toAjax(ptReportService.expReportGetScore(ptReportVO));
    }

}
